package com.tomcat.server;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.security.Principal;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;

/**
 * desc: 把请求信息封装为request对象
 *
 * @author tengfei wang
 * @version 1.0
 * @date Created in 24/9/2020
 */
public class Request implements HttpServletRequest {

    /**
     * 请求方法
     */
    private String method;
    /**
     * host
     */
    private String serviceName;
    /**
     * context
     */
    private String contextPath;

    /**
     * servlet path
     */
    private String servletPath;

    private String requestURI;

    private InputStream inputStream;

    @Override
    public String getMethod() {
        return this.method.trim();
    }

    public String getServiceName() {
        return this.serviceName.trim();
    }

    @Override
    public String getServletPath() {
        return this.servletPath.trim();
    }

    @Override
    public String getContextPath() {
        return this.contextPath.trim();
    }

    @Override
    public String getRequestURI() {
        return this.requestURI.trim();
    }

    /**
     * 解析InputStream ，拿到请求方法和路径
     *
     * @param inputStream 输入流
     * @throws IOException 异常
     */
    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int count = 0;
        // 数据有可能有延迟，一直读取，直到有数据为止
        while (count == 0) {
            count = inputStream.available();
        }

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String s;
        StringBuilder sb = new StringBuilder();
        while ((s = bufferedReader.readLine()).length() != 0) {
            sb.append(s).append("\n");
        }
        String[] split = sb.toString().split("\n");
        //拿到第一行数据
        String[] firstLine = split[0].split(" ");
        //填充method
        this.method = firstLine[0];
        this.requestURI = firstLine[1];
        if ("/".equals(requestURI)) {
            this.contextPath = "";
            this.servletPath = "";
        } else {
            String[] pathArr = requestURI.substring(1).split("/");
            // 填充 contextPath 和 servletPath
            this.contextPath = pathArr[0];
            this.servletPath = requestURI.substring(pathArr[0].length() + 1);
        }
        for (int i = 1; i < split.length; i++) {
            String[] var1 = split[i].split(":");
            if ("host".equalsIgnoreCase(var1[0])) {
                // 填充serviceName
                this.serviceName = var1[1];
            }
        }
    }


    @Override
    public String getAuthType() {
        return null;
    }

    @Override
    public Cookie[] getCookies() {
        return new Cookie[0];
    }

    @Override
    public long getDateHeader(String s) {
        return 0;
    }

    @Override
    public String getHeader(String s) {
        return null;
    }

    @Override
    public Enumeration getHeaders(String s) {
        return null;
    }

    @Override
    public Enumeration getHeaderNames() {
        return null;
    }

    @Override
    public int getIntHeader(String s) {
        return 0;
    }

    @Override
    public String getPathInfo() {
        return null;
    }

    @Override
    public String getPathTranslated() {
        return null;
    }

    @Override
    public String getQueryString() {
        return null;
    }

    @Override
    public String getRemoteUser() {
        return null;
    }

    @Override
    public boolean isUserInRole(String s) {
        return false;
    }

    @Override
    public Principal getUserPrincipal() {
        return null;
    }

    @Override
    public String getRequestedSessionId() {
        return null;
    }


    @Override
    public StringBuffer getRequestURL() {
        return null;
    }

    @Override
    public HttpSession getSession(boolean b) {
        return null;
    }

    @Override
    public HttpSession getSession() {
        return null;
    }

    @Override
    public boolean isRequestedSessionIdValid() {
        return false;
    }

    @Override
    public boolean isRequestedSessionIdFromCookie() {
        return false;
    }

    @Override
    public boolean isRequestedSessionIdFromURL() {
        return false;
    }

    @Override
    public boolean isRequestedSessionIdFromUrl() {
        return false;
    }

    @Override
    public Object getAttribute(String s) {
        return null;
    }

    @Override
    public Enumeration getAttributeNames() {
        return null;
    }

    @Override
    public String getCharacterEncoding() {
        return null;
    }

    @Override
    public void setCharacterEncoding(String s) throws UnsupportedEncodingException {

    }

    @Override
    public int getContentLength() {
        return 0;
    }

    @Override
    public String getContentType() {
        return null;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        return null;
    }

    @Override
    public String getParameter(String s) {
        return null;
    }

    @Override
    public Enumeration getParameterNames() {
        return null;
    }

    @Override
    public String[] getParameterValues(String s) {
        return new String[0];
    }

    @Override
    public Map getParameterMap() {
        return null;
    }

    @Override
    public String getProtocol() {
        return null;
    }

    @Override
    public String getScheme() {
        return null;
    }

    @Override
    public String getServerName() {
        return null;
    }

    @Override
    public int getServerPort() {
        return 0;
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return null;
    }

    @Override
    public String getRemoteAddr() {
        return null;
    }

    @Override
    public String getRemoteHost() {
        return null;
    }

    @Override
    public void setAttribute(String s, Object o) {

    }

    @Override
    public void removeAttribute(String s) {

    }

    @Override
    public Locale getLocale() {
        return null;
    }

    @Override
    public Enumeration getLocales() {
        return null;
    }

    @Override
    public boolean isSecure() {
        return false;
    }

    @Override
    public RequestDispatcher getRequestDispatcher(String s) {
        return null;
    }

    @Override
    public String getRealPath(String s) {
        return null;
    }

    @Override
    public int getRemotePort() {
        return 0;
    }

    @Override
    public String getLocalName() {
        return null;
    }

    @Override
    public String getLocalAddr() {
        return null;
    }

    @Override
    public int getLocalPort() {
        return 0;
    }
}
